<template>
  <div class="my">
    <!-- 背景 -->
    <van-image
      width="100%"
      height="260px"
      :src="bgc"
    />

    <div class="userLogin">
      <div class="pic">
        <div class="headPicture">
          <van-image
            round
            width="70px"
            height="70px"
            :src="avatar"
          />
        </div>
      </div>
      <div class="uname">
        {{nickname}}
      </div>
      <van-button class="btn" type="info" block @click="loginBtn">{{loginTxt}}</van-button>
      <p class="bianji" v-if="!isShown">编辑个人资料></p>
    </div>
    <!-- 功能 -->
    <div class="module" v-if="isShown">
      <van-grid clickable :column-num="3" :border="false">
        <!-- 点击时触发click -->
        <van-grid-item icon="star-o"  text="我的收藏" to="/login" />
        <van-grid-item icon="wap-home-o" text="我的出租" to="/login" />
        <van-grid-item icon="clock-o" text="看房记录" />
        <van-grid-item icon="credit-pay" text="成为房主" />
        <van-grid-item icon="contact" text="个人资料"/>
        <van-grid-item icon="phone-o" text="联系我们" />
      </van-grid>
    </div>

    <div class="module" v-else>
      <van-grid clickable :column-num="3" :border="false">
        <!-- 点击时触发click -->
        <van-grid-item icon="star-o"  text="我的收藏" to="/collect" />
        <van-grid-item icon="wap-home-o" text="我的出租" to="/myhouse" />
        <van-grid-item icon="clock-o" text="看房记录" />
        <van-grid-item icon="credit-pay" text="成为房主" />
        <van-grid-item icon="contact" text="个人资料"/>
        <van-grid-item icon="phone-o" text="联系我们" />
      </van-grid>
    </div>

    <div class="join">
      <van-image
        class="imgjoin"
        width="90%"
        height="100%"
        src="http://liufusong.top:8080/img/profile/join.png"
      />
    </div>
  </div>

</template>
<script>
import { userInfo } from '@/api/user'
import { Dialog } from 'vant'
import { delToken, getToken } from '@/utils/storage'
export default {
  name: 'MyHouse',
  data () {
    return {
      isShown: true,
      nickname: '游客',
      avatar: 'http://liufusong.top:8080/img/profile/avatar.png',
      loginTxt: '去登录',
      bgc: 'http://liufusong.top:8080/img/profile/bg.png'
    }
  },
  async created () {
    // 获取用户信息
    if (getToken()) {
      const { body: { nickname, avatar } } = await userInfo()
      this.nickname = nickname
      this.avatar = 'http://liufusong.top:8080' + avatar
      this.bgc = 'http://liufusong.top:8080' + avatar
      this.loginTxt = '退出登录'
      this.isShown = false
    }
  },
  methods: {
    loginBtn () {
      if (getToken()) {
        Dialog.confirm({
          title: '',
          message: '确认退出登录？'
        })
          .then(() => {
            delToken()
            this.isShown = !this.isShown
            // 实现跳转自己
            this.$router.go('0')
          // on confirm
          })
          .catch(() => {
          // on cancel
          })
      } else {
        this.$router.push('/login')
      }
    }
  }
}
</script>

<style lang = "less" scoped>
.my{
  position: relative;
  width: 100%;
  .module{
    margin-top: 50px;
  }
  .userLogin{
  position: absolute;
  z-index: 99;
  top:130px;
  left: 50%;
  width: 90%;
  transform: translate(-50%);
  height: 160px;
  background-color: #fff;
  box-shadow: 0 0 10px 3px #ddd
}
.join{
  display: flex;
  margin-top: 20px;
  width: 100%;
  .imgjoin{
    margin: auto;
  }
}
.pic{
  width: 86px;
  height: 86px;
  border-radius: 50%;
  background-color: #f5f5f5;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%,-50%);
  .headPicture{
    position: absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }
}
.btn{
  border-radius: 8px;
  width: 80px;
  height: 30px;
  font-size: 12px;
  margin: 75px auto;
}
.uname{
  position: absolute;
  top:35%;
  left: 50%;
  transform: translate(-50%,-50%);
  font-size:12px
}
.bianji{
  font-size: 12px;
  color: #4d4b4b;
  position: absolute;
  top: 70%;
  left: 50%;
  transform: translate(-50%);
}
}
</style>
